package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        try {
            Connection connection=DBUtil.getConnection();
            Statement statement=connection.createStatement();
            String sql="SELECT t.name,t.salary,t.title,c.name,c.floor FROM teacher t JOIN class c ON t.id=c.teacher_id WHERE t.name='王克晶'";
            System.out.println(sql);
            ResultSet resultSet= statement.executeQuery(sql);
            while (resultSet.next()){
//                String name=resultSet.getString("name");
//                double salary=resultSet.getDouble("salary");
//                String title=resultSet.getString("title");
//                String className=resultSet.getString("name");
//                int floor=resultSet.getInt("floor");
                //获取字段值时可以使用字段索引，注意：从1开始为结果集第一个字段。可读性差不推荐
                String name=resultSet.getString(1);
                double salary=resultSet.getDouble(2);
                String title=resultSet.getString(3);
                String className=resultSet.getString(4);
                int floor=resultSet.getInt(5);
                System.out.println(name+" "+salary+" "+title+" "+className+" "+floor);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
